package org.phoenix.mall.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.phoenix.mall.admin.common.pojo.vo.AttributeVO;
import org.phoenix.mall.admin.common.utils.PhoenixResponseJSON;
import org.phoenix.mall.admin.config.annotations.AuthRequired;
import org.phoenix.mall.admin.pojo.Attribute;
import org.phoenix.mall.admin.service.AttributeService;
import org.phoenix.mall.admin.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(value = "分类管理", tags = {"分类管理API"})
@RestController
@RequestMapping("categories")
@AuthRequired
public class CategoryController {

    private CategoryService categoryService;
    private AttributeService attributeService;

    @ApiOperation(value = "获取分类列表", notes = "获取分类列表", httpMethod = "GET")
    @GetMapping("")
    public PhoenixResponseJSON list(
            @ApiParam(name = "type", value = "查询条件", defaultValue = "1")
            @RequestParam(required = false) Integer type,
            @ApiParam(name = "pagenum", value = "页码")
            @RequestParam(required = false) Integer pagenum,
            @ApiParam(name = "pagesize", value = "每页显示记录数")
            @RequestParam(required = false) Integer pagesize) {

        if (type != null && type == 3 && pagenum != null && pagesize != null) {
            return PhoenixResponseJSON.ok(categoryService.getCategoryListByPage(pagenum, pagesize));
        } else {
            if (type == null) {
                type = 3;
            }
//            List<CategoryVO> list = BuildObjUtil.build("data/example.json",CategoryVO.class);
//            return PhoenixResponseJSON.ok(list);
            return PhoenixResponseJSON.ok(categoryService.getChildCategoryList(0, type - 1));
        }
    }

    @ApiOperation(value = "添加分类参数", notes = "添加分类参数", httpMethod = "POST")
    @PostMapping("/{id}/attributes")
    public PhoenixResponseJSON add(@PathVariable Integer id, @RequestBody Map<String, Object> map) {
        Object attrNameObj = map.get("attrName");
        String attrName = attrNameObj == null ? null : attrNameObj.toString();
        Object attrSelObj = map.get("attrSel");
        String attrSel = attrSelObj == null ? null : attrSelObj.toString();

        Attribute attribute = new Attribute();
        attribute.setAttrName(attrName);
        attribute.setCategoryId(id);
        attribute.setAttrSel(attrSel);
        attribute.setAttrWrite("manual");
        attribute.setCreateTime(new Date());
        int count = attributeService.create(attribute);
        if (count == 0) {
            return PhoenixResponseJSON.errorMsg("添加分类参数出错！");
        }
        return PhoenixResponseJSON.ok();
    }

    @ApiOperation(value = "分类参数列表", notes = "分类参数列表", httpMethod = "GET")
    @GetMapping("/{id}/attributes")
    public PhoenixResponseJSON list(@PathVariable Integer id, @RequestParam String attrSel) {
        List<AttributeVO> list = attributeService.getAttributeList(id, attrSel);
        return PhoenixResponseJSON.ok(list);
    }

    @Resource
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Resource
    public void setAttributeService(AttributeService attributeService) {
        this.attributeService = attributeService;
    }
}
